/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.deleteUser;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
 * View class for the DeleteUser use case
 * @author Administrator
 */
public class DeleteUserView extends JFrame{
    private final static String userFieldParse = "mmmmmmmmmm";
    private final static String passFieldParse = "mmmmmmmmmm";
    /**
     * Exit type
     */
    public final static int EXIT_TYPE = JFrame.DO_NOTHING_ON_CLOSE;
    
    private DeleteUserModel model;
    private static boolean running = false;
    
    private JLabel usernameLabel;      
    private JTextField usernameField;      
    private JLabel passwordLabel;
    private JPasswordField passwordField;
    private JButton deleteButton;
    private JButton cancelButton;
    
    /**
     * Default constructor calls Constructor(model) with as new model
     */
    public DeleteUserView(){
        this(new DeleteUserModel());
    }
    /**
     * Constructor that sets the model to the input and creates the form
     * @param model
     */
    public DeleteUserView(DeleteUserModel model){
        this.model = model;
        createFrame();
    }
    /**
     * Shows the gui and sets running to true
     */
    public void showGUI(){
        running = true;
        setVisible(true);
    }
    /**
     * Hides the gui and sets running to false
     */
    public void hideGUI(){
        running = false;
        setVisible(false);
    }
    /**
     * Returns the value stored in the running variable
     * @return
     * running
     */
    public boolean isRunning() {
        return running;
    }
    /**
     * Sets running to the input boolean
     * @param b
     * running = b
     */
    public void setRunning(boolean b) {
        running = b;
    }
    /**
     * Function to create the delete frame
     * Has fields for username and password
     */
    public void createFrame(){
        GridLayout grid = new GridLayout(0,2);
        setLayout(grid);
        
        //create buttons, labels etc
        usernameLabel = new JLabel("Enter Username:");
        usernameLabel.setHorizontalAlignment(SwingConstants.LEFT);        

        usernameField = new JTextField();        
        setFieldWidth(usernameField,userFieldParse);
              
        
        passwordLabel = new JLabel("Enter Password:");
        passwordLabel.setHorizontalAlignment(SwingConstants.LEFT);            
        
        passwordField = new JPasswordField();
        setFieldWidth(passwordField,passFieldParse);
        
        JPanel deleteButtonPanel = new JPanel(new FlowLayout());
        
        deleteButton = new JButton("Delete");       
        deleteButtonPanel.add(deleteButton);
        
        cancelButton = new JButton("Cancel");
        deleteButtonPanel.add(cancelButton);
        
        //first row username lable and field
        add(usernameLabel); add(usernameField);
        //second row password lable and field
        add(passwordLabel); add(passwordField);
        //third row blank pannel and login button panel
        add(new JPanel()); add(deleteButtonPanel);
        
        //display        
        setTitle("Delete User");
        setDefaultCloseOperation(EXIT_TYPE);
        setAlwaysOnTop(true);
        setResizable(false);
        pack();
        setLocationRelativeTo(null); //center frame
    }
    /**
     * Set the commands and listener for the buttons
     * @param a
     * Action listener to be added to the buttons
     */
    public void buttonListener (ActionListener a){
        cancelButton.setActionCommand(model.CANCEL);
        cancelButton.addActionListener(a);
        
        deleteButton.setActionCommand(model.DELETE);
        deleteButton.addActionListener(a);
    }
    /**
     * Sets the size of the field to the size generated by the string length then clears the field
     * @param field
     * Field to be set
     * @param length
     * String that is the length that is required
     */
    public void setFieldWidth(JTextField field, String length){
        field.setText(length);
        field.setPreferredSize(field.getPreferredSize());
        field.setText("");
    }
    /**
     * Create a message dialog with the same inputs as the showmessagedialog function.
     * Used for ease when creating messages
     * @param parentcomponent
     * @param message
     * Message to be displayed
     * @param title
     * Title of the dialog
     * @param messageType
     * Type of message
     */
    public void message(Component parentcomponent,
            Object message,
            String title,
            int messageType ){            
        JOptionPane.showMessageDialog(parentcomponent,message,title,messageType);
    }
    /**
     * Create an invaild user/pass message box
     */
    public void errorMessageBox(){
        message(this,"Invalid username or password",
        "Invalid User",
        JOptionPane.ERROR_MESSAGE);
    }
    /**
     * Create a success message box
     */
    public void successMessageBox(){
        message(this,"Account was successfully Deleted",
        "Account Deleted",
        JOptionPane.INFORMATION_MESSAGE);
    }
    /**
     * Create a cancel message box
     */
    public void canceledMessageBox(){
        message(this,"Delete has been canceled",
        "Canceled",
        JOptionPane.WARNING_MESSAGE);
    }
    /**
     * Get the username from the field text
     * @return
     * Username as a string
     */
    public String getUsernameField(){return usernameField.getText();}
    /**
     * Get the password from the field text
     * @return
     * Password from password field as a character array
     */
    public char[] getPasswordField(){return passwordField.getPassword();}
    /**
     * Clear the username and password fields
     */
    public void clearUsernamePasswordField(){
        usernameField.setText("");
        passwordField.setText("");
    }
}
